import Vue from 'vue'
import VueRouter from 'vue-router'
//一级路由
import goodsdetails from '../pages/goodsdetails.vue'
import Index from '../pages/index.vue'
import Order from '../pages/order.vue'
import Search from '../pages/search.vue'
import goodslist from '../pages/goodslist.vue'
import Login from '../pages/login.vue'
import Register from '../pages/register.vue'

//二级路由
import Home from '../views/home.vue'
import Shopping from '../views/shopping.vue'
import Twosort from '../views/sort.vue'
import User from '../views/user.vue'
Vue.use(VueRouter)
// const Home = ()=> import('../views/home.vue')
const router = new VueRouter({
  routes:[
    //首页
    {
      path:"/index",
      component:Index,
      meta:{
        title:'首页'
      },
      children:[
        //主页
        {
          path:'/home',
          component:()=> import('../views/home.vue'),
          alias:'/zhuye',
          meta:{
            title:'小U商城'
          },//别名
        },
        //购物车
        {
          path:'/shopping',
          component:()=> import('../views/shopping.vue'),
          meta:{
            title:'购物车'
          },
        },
        //商品详情
        {
          path:'/twosort',
          component:()=> import('../views/sort.vue'),
          meta:{
            title:'分类'
          },
        },
        //个人
        {
          path:'/user',
          component:()=> import('../views/user.vue'),
          meta:{
            title:'我的'
          },
        },
        {
          path:"",
          redirect:'/home'
        }
      ]
    },
    //订单
    {
      path:'/order',
      component:()=> import('../pages/order.vue'),
      meta:{
        title:'订单详情'
      },
    },
    //搜索
    {
      path:'/search',
      component:()=> import('../pages/search.vue'),
      meta:{
        title:'搜索页'
      },
    },
    //商品列表
    {
      path:'/goodslist',
      component:()=> import('../pages/goodslist.vue'),
      meta:{
        title:'小U商城'
      },
    },
    //商品详情
    {
      path:'/goodsdetails',
      component:()=> import('../pages/goodsdetails.vue'),
      meta:{
        title:'商品详情'
      },
    },
    //登录
    {
      path:'/login',
      component:()=> import('../pages/login.vue'),
      meta:{
        title:'手机登录'
      },
    }, 
    
    //注册
    {
      path:'/register',
      component:()=> import('../pages/register.vue'),
      meta:{
        title:'手机注册'
      },
    },
    {
      path:'*',
      redirect:'/index'
    }
  ],
  //登录导航守卫拦截
 
})
router.beforeEach((to,from,next)=>{
  if(to.path=='/login'){
    next()
    return
  }
  if(to.path=='/register'){
    next()
    return
  }
  if(sessionStorage.getItem('isLogin')){
    next()
    return
  }
  next('/login')
})
export default router
